<?php defined('SYSPATH') OR die('No direct access allowed.');

class Product_Controller extends Template_Controller {
	const ALLOW_PRODUCTION = FALSE;
	public $template = 'layouts/template_admin';

	public function index(){
		$this->template->content = new View('product/create');
		$this->template->content->captcha = Captcha::factory();
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->title = 'เพิ่มสินค้า';
	}

	public function create(){
		$this->template->content = new View('product/create');
		$this->template->content->captcha = Captcha::factory();
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->title = 'เพิ่มสินค้า';

	}
	public function insert(){

		$files1 = Validation::factory($_FILES)
		->add_rules('picture1', 'upload::valid', 'upload::required', 'upload::type[gif,jpg,png]', 'upload::size[1M]');
		$files2 = Validation::factory($_FILES)
		->add_rules('picture2', 'upload::valid', 'upload::required', 'upload::type[gif,jpg,png]', 'upload::size[1M]');
		$files3 = Validation::factory($_FILES)
		->add_rules('picture3', 'upload::valid', 'upload::required', 'upload::type[gif,jpg,png]', 'upload::size[1M]');

		$fileTemp1="";
		$fileTempThumbnail1="";
		$fileTemp2="";
		$fileTempThumbnail2="";
		$fileTemp3="";
		$fileTempThumbnail3="";
		if ($files1->validate()){
			// Temporary file name
			$filename1 = upload::save('picture1');
			// Resize, sharpen, and save the image
			Image::factory($filename1)
			->save(DOCROOT.'product/pictures/'.basename($filename1));
			// Resize, sharpen, and save the image
			Image::factory($filename1)
			->resize(100, 130, Image::WIDTH)
			->save(DOCROOT.'product/pictures/thumbnails/'.basename($filename1));

			// File name 1
			$fileTemp1='product/pictures/'.basename($filename1);
			$fileTempThumbnail1='product/pictures/thumbnails/'.basename($filename1);
			// Remove the temporary file
			unlink($filename1);
		}
		if ($files2->validate()){
			// Temporary file name
			$filename2 = upload::save('picture2');
			// Resize, sharpen, and save the image
			Image::factory($filename2)
			->save(DOCROOT.'product/pictures/'.basename($filename2));
			// Resize, sharpen, and save the image
			Image::factory($filename2)
			->resize(150, 150, Image::WIDTH)
			->save(DOCROOT.'product/pictures/thumbnails/'.basename($filename2));

			// File name 2
			$fileTemp2='product/pictures/'.basename($filename2);
			$fileTempThumbnail2='product/pictures/thumbnails/'.basename($filename2);

			// Remove the temporary file
			unlink($filename2);
		}
		if ($files3->validate()){
			// Temporary file name
			$filename3 = upload::save('picture3');
			// Resize, sharpen, and save the image
			Image::factory($filename3)
			->save(DOCROOT.'product/pictures/'.basename($filename3));
			// Resize, sharpen, and save the image
			Image::factory($filename3)
			->resize(150, 150, Image::WIDTH)
			->save(DOCROOT.'product/pictures/thumbnails/'.basename($filename3));

			// File name 3
			$fileTemp3='product/pictures/'.basename($filename3);
			$fileTempThumbnail3='product/pictures/thumbnails/'.basename($filename3);

			// Remove the temporary file
			unlink($filename3);
		}

		//echo $fileTemp1;
		//echo $fileTemp2;
		//echo $fileTemp3;

		$product = new Product_Model();
		$product->name=$this->input->post("name");
		$product->description=$this->input->post("description");
		$product->price=$this->input->post("price");
		$product->category_id=$this->input->post("category_id");
		$product->image=$fileTemp1;
		$product->thumbnail=$fileTempThumbnail1;
		$product->save();

		if($fileTemp1!=""){
			$productImage = new Product_Image_Model();
			$productImage->description="";
			$productImage->image=$fileTemp1;
			$productImage->thumbnail=$fileTempThumbnail1;
			$productImage->product_id=$product->id;
			$productImage->save();
		}
		if($fileTemp2!=""){
			$productImage = new Product_Image_Model();
			$productImage->description="";
			$productImage->image=$fileTemp2;
			$productImage->thumbnail=$fileTempThumbnail2;
			$productImage->product_id=$product->id;
			$productImage->save();
		}
		if($fileTemp3!=""){
			$productImage = new Product_Image_Model();
			$productImage->description="";
			$productImage->image=$fileTemp3;
			$productImage->thumbnail=$fileTempThumbnail3;
			$productImage->product_id=$product->id;
			$productImage->save();
		}
		url::redirect("product/product/create");
	}
	
	public function listResult(){
		$this->template->content = new View('product/listResult');
		$this->template->content->products=ORM::factory("product")->find_all();
		$this->template->title = 'แสดงรายการ สินค้า ทั้งหมด';
	}
}
?>
